frames reset
capture postutil clear
scalar drop _all 
matrix drop _all
mata: mata clear 
cd "${temp}"

local data "${temp}"

local occupations physicians pcps lawyers 

local incvars ptotinc 
	
local beta  97

local measures NPV

local kernel triangular

local depths 1 

set graphics off

postfile means str20 occ str21 incvar str12 measure str4 beta value N_UR using "`data'/national_means_simulated.dta", replace every(1)

foreach depth in `depths'{
foreach oc in `occupations' {
	foreach i in `incvars' {
		local cc = `cc'+1
		clear 
		foreach b in `beta' {
			use "`data'/simulated_npvs_`oc'_`i'_`depth'years_singleyear_`kernel'_b`b'.dta" , clear
			replace NPV_d0`b' = NPV_d0`b'/1000000
		}
		foreach b in `beta' {
			foreach m in `measures' {
				qui sum `m'_d0`b', d
				local N_UR = r(N)
				local mean = r(mean)
				local p5 = r(p1)
				local p95 = r(p95)
				
				if "`m'"=="NPV" local mm = "GPV"
				
				post means  ("`oc'") ("`i'") ("`mm'") ("0.`b'") (`mean') (`N_UR')

			}
		}
		
	}
}
}

use "${temp}/national_means_simulated.dta", clear
replace value = round(value,.1)
destring beta, replace
gen type = "means"
compress

ren N_UR n_ur_temp
egen N_UR = max(n_ur_temp)
drop n_ur_temp
drbcount N_UR , gen(N)
drbest value, replace
keep if type == "means"
order N_UR, last
export delimited using "${mypath}/intermediate_csv/pdv03_pdvs_physicians_pcps_lawyers.csv", replace
